/**********************************************************************************
// DemoBitmap
// 
// Creation:	02 Jul 2007
// Updated:		29 Apr 2011
// Compiler:	Visual Studio 2010
//
// Notes:		Test the bitmap loading capabilities
//				Draw and move a bitmap pixel by pixel
//
**********************************************************************************/

#define WIN32_LEAN_AND_MEAN  // just say no to MFC

#include "Resources.h"
#include "Anabele.h"	


/**********************************************************************************/
/*                         BitmapDemo Class                                       */
/**********************************************************************************/


class BitmapDemo : public D3DGame
{
private:
	int x, y, dirX, dirY;
	Bitmap * bmp;
	Sprite * demobox;
	char text[80];
public:
	void GameInit();
	void GameLogic();
	void GameDraw();
	void GameShutDown();
	void UnloadResources();
	void LoadResources();
};

/**********************************************************************************/

void BitmapDemo::GameInit()
{
	bmp = new Bitmap("Resources\\CarKaraCanvas.bmp");

	x = (GetWindowWidth()/2) - (bmp->GetImageWidth()/2);
	y = (GetWindowHeight()/2) - (bmp->GetImageHeight()/2);

	dirX = 5;
	dirY = 5;

	LoadResources();
}

/**********************************************************************************/

void BitmapDemo::LoadResources()
{
	demobox = new Sprite("Resources/demoBox.png");
	demobox->MoveTo(155, 100);
}

/**********************************************************************************/

void BitmapDemo::UnloadResources()
{
	if (demobox) delete demobox;
}

/**********************************************************************************/

void BitmapDemo::GameShutDown()
{
	UnloadResources();
}

/**********************************************************************************/

void BitmapDemo::GameLogic()
{
	// for now test if user is hitting ESC and send WM_CLOSE
	EndsOnEscape();

	x += dirX;
	y += dirY;
	
	if (x+100 < 0)
	{	
		dirX = -dirX;
	}

	if (y+100 < 0)
	{
		dirY = -dirY;
	}

	if (x + bmp->GetImageWidth()-100 > GetWindowWidth())
	{
		dirX = -dirX;
	}

	if (y + bmp->GetImageHeight()-100 > GetWindowHeight())
	{
		dirY = -dirY;		
	}
} 

/**********************************************************************************/

void BitmapDemo::GameDraw()
{
	// lock screen for pixel plotting
	LockScreen();
	Draw(*bmp, x, y);
	UnLockScreen();

	demobox->Draw();

	sprintf_s(text, "Bitmap Demo");
	sysFont->Draw(55, 40, text);

	sprintf_s(text, "Loading bitmap image from disk");
	sysFont->Draw(55, 80, text);

	sprintf_s(text, "Drawing bitmap pixel by pixel");
	sysFont->Draw(55, 100, text);

	sprintf_s(text, "Clipping out of screen pixels");
	sysFont->Draw(55, 120, text);

	sprintf_s(text, "Image position: %d x %d", x, y);
	sysFont->Draw(55, 140, text);
} 



/**********************************************************************************/
/*                                  WinMain                                       */
/**********************************************************************************/

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	BitmapDemo bmpDemo;

	bmpDemo.SetWindowBgColor(16,16,16);
	bmpDemo.SetWindowTitle(string("Bitmap Demo"));
	bmpDemo.SetWindowIcon(LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)));
	bmpDemo.ShowWindowCursor(false);
	bmpDemo.Initialize(hInstance);
	bmpDemo.Start();
	
	return 0;

}

/**********************************************************************************/
